<?php

class Controller_RotationMap extends Controller_Website {

    public function action_list() {
        $where = array();
        $where['status'] = Arr::get($_GET, 'status');
        $where['category'] = Arr::get($_GET, 'category');
        $where['ORDER'] = 'id DESC';
        $where = array_filter($where, 'strlen');
  
        $m_rotationMap = Model::factory('rotationMap');
        $total = $m_rotationMap->count($where);
        $pager = new Pager($total, 5);
        $list = $m_rotationMap->select($pager->offset, $pager->size, $where)->as_array();
        
        $this->content = View::factory('rotationMap/list');
        $this->content->list = $list;
        $this->content->pager = $pager;


    }

    public function action_add() {
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $data['add_time'] = time();
            $m_rotationMap = Model::factory('rotationMap');
            $ret = $m_rotationMap->insert($data);
            if ($ret !== false) {
                $this->redirect('rotationMap/list');
            }
        }

        $this->content = View::factory('rotationMap/add');
        $this->content->info = null;
        

    }

    public function action_edit() {
        $id = Arr::get($_GET, 'id');
        $m_rotationMap = Model::factory('rotationMap');
        $info = $m_rotationMap->getRowById($id);
        
        if (!empty($_POST)) {
            $data = $this->_get_data($_POST);
            $ret = $m_rotationMap->updateById($data, $id);
            if ($ret !== false) {
                $this->redirect('rotationMap/list');
            }
        }

        $this->content = View::factory('rotationMap/edit');
        $this->content->info = $info;
    }

    public function action_del() {
        $id = $_GET['id'];
        $m_rotationMap = Model::factory('rotationMap');
        $ret = $m_rotationMap->deleteById($id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    public function action_close() {
        $id = $_GET['id'];
        $data = array(
            'status' => '0',
        );
        $m_rotationMap = Model::factory('rotationMap');
        $ret = $m_rotationMap->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_open() {
        $id = $_GET['id'];
        $data = array(
            'status' => '1',
        );
        $m_rotationMap = Model::factory('rotationMap');
        $ret = $m_rotationMap->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_category1() {
        $id = $_GET['id'];
        $data = array(
            'category' => '2',
        );
        $m_rotationMap = Model::factory('rotationMap');
        $ret = $m_rotationMap->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }
    
    public function action_category2() {
        $id = $_GET['id'];
        $data = array(
            'category' => '1',
        );
        $m_rotationMap = Model::factory('rotationMap');
        $ret = $m_rotationMap->updateById($data, $id);
        if ($ret !== false) {
            $this->redirect(Request::$referrer);
        }
    }

    protected function _get_data($post) {
        $data = array_intersect_key($post, array_flip(array('pic','jump_url','desc','category')));
        if (!empty($data['pic'])) {
            $data['pic'] = array_pop($data['pic']);
        }
        return $data;
    }
    
}
